/**
 * Created by WangMing on 15/12/9.
 */

define([
  "avalon",
  'text!./tab_widget.html',
  '../../service',
  "imports?$=jquery!../../assets/vendor/bootstrap/bootstrap-tab"
  ], function (avalon,sourceHTML,api) {
  // console.log(sourceHTML);
  // 定义widget
  var widget = avalon.ui.tabWidget = function(element, data, vmodels){
  	

    var vmodel = avalon.define(data.tabWidgetId, function(vm){


      vm.tab = {
         active: 1
      };
      vm.params = {
        method: api.getVideos,
        query:{
            order: null,
            queryString: []
        }
      };

      vm.size = 4;

      avalon.mix(vm, data.tabWidgetOptions);

      vm.currentOrder={}
      vm.$skipArray = ["orders","currentOrder","params"]

  		vm.$init = function(){
          // order by tab
          console.log("tab_widget",$,jQuery,jquery);
          //
          if(!_.has(vm.params.query, "queryString")){
            vm.params.query.queryString = [];
          }
          vm.params.query.queryString.push("size="+vm.size);
  
          avalon.each(vm.orders,function(index,o){
          
            // video by orders
            
            api.getItems(avalon.mix( vm.params ,{query:o.query}),function(res){

            
              o.order = [];
             
              // stub


              o.order = res.collections;
              if(o.order.length > vm.size){
                o.order = o.order.slice(0,vm.size);
              }
              element.innerHTML = sourceHTML;
              avalon.scan(element, [vmodel].concat(vmodels));
               
              jQuery('.tabWidget a:first').tab('show');  



            })




          })

        


  		}

  		vm.$remove = function(){
  			element.innerHtml = element.textContent = "";
  		}
  	})
    return vmodel;
  }
  widget.defaults = {
   orders:[{
    title:"周排行",
    target:"weekPanel",
    query: {
      order: "order by week"
    },
    order:[]
    },{
      title:"年排行",
      target:"yearPanel",
      query: {
        order: "order by lastest"
      },
      order:[]
    }]
  }

  return avalon;
});

